package co.fitstart.entity.coach;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.user.Student;

/**
 * @author downpour
 *
 */
@Entity
@Table(name = "coach_student_relation")
@DynamicUpdate
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class CoachStudentRelation {
    
    @Id
    @GeneratedValue
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Principal coach;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Student student;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private CourseSchedule courseSchedule;
    
    /** 关系所属课程类型 */
    @Enumerated(EnumType.STRING)
    private CourseType courseType;
    
    /** 已签到的课时数 */
    private int signedAmount;
    
    /** 最后更新时间 */
    private Date lastUpdateTime;

    /** 创建时间 */
    private Date createTime;
    
    /**
     * The default constructor
     */
    public CoachStudentRelation() {
    }
    
    /**
     * Create CoachStudentRelation by principal / courseSchedule
     * @param coach
     * @param courseSchedule
     */
    public CoachStudentRelation(Principal coach, CourseSchedule courseSchedule) {
        this.coach = coach;
        this.courseSchedule = courseSchedule;
        this.student = courseSchedule.getStudent();
        this.courseType = courseSchedule.getCourseType();
        
        this.signedAmount = 0;
        
        this.createTime = new Date();
        this.lastUpdateTime = this.createTime;
    }
    
    /**
     * 
     * @param courseSchedule
     * @return
     */
    public CoachStudentRelation editSchedule(CourseSchedule courseSchedule) {
        this.courseSchedule = courseSchedule;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public CoachStudentRelation updateTime() {
        this.signedAmount = this.signedAmount + 1;
        this.lastUpdateTime = new Date();
        return this;
    }

    /**
     * 
     * @return
     */
    public String getStudentAvatar() {
        return student.getUser().getProfile() == null ? null : student.getUser().getProfile().getAvatar();
    }

    /**
     * 
     * @return
     */
    public String getStudentName() {
        return student.getName();
    }

    /**
     * 
     * @return
     */
    public String getStuentMobile() {
        return student.getMobile();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the coach
     */
    public Principal getCoach() {
        return coach;
    }

    /**
     * @return the student
     */
    public Student getStudent() {
        return student;
    }

    /**
     * @return the courseSchedule
     */
    public CourseSchedule getCourseSchedule() {
        return courseSchedule;
    }

    /**
     * @return the courseType
     */
    public CourseType getCourseType() {
        return courseType;
    }

    /**
     * @return the signedAmount
     */
    public int getSignedAmount() {
        return signedAmount;
    }

    /**
     * @return the lastUpdateTime
     */
    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param coach the coach to set
     */
    public void setCoach(Principal coach) {
        this.coach = coach;
    }

    /**
     * @param student the student to set
     */
    public void setStudent(Student student) {
        this.student = student;
    }

    /**
     * @param courseSchedule the courseSchedule to set
     */
    public void setCourseSchedule(CourseSchedule courseSchedule) {
        this.courseSchedule = courseSchedule;
    }

    /**
     * @param courseType the courseType to set
     */
    public void setCourseType(CourseType courseType) {
        this.courseType = courseType;
    }

    /**
     * @param signedAmount the signedAmount to set
     */
    public void setSignedAmount(int signedAmount) {
        this.signedAmount = signedAmount;
    }

    /**
     * @param lastUpdateTime the lastUpdateTime to set
     */
    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}
